package com.atguigu.gmall.order.listener;

import com.atguigu.gmall.task.config.MqConst;
import com.atguigu.gmall.order.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author: 李旭
 * @create: 2021-07-26 10:31
 **/
@Component
public class OrderInfoListener {

    @Autowired
    private OrderInfoService orderInfoService;

    //取消订单
    @RabbitListener(queues = MqConst.QUEUE_ORDER_CANCEL)
    public void cancelOrder(Long orderId, Channel channel, Message message){
        System.out.println("订单的ID:" + orderId + ":时间:" + new Date());
        try {
            //取消订单
            orderInfoService.cancelOrderInfo(orderId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);//
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //更新订单状态
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_PAYMENT_PAY,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_PAYMENT_PAY),
            key = MqConst.ROUTING_PAYMENT_PAY
    ))
    public void updateOrderStatus(Long orderId, Channel channel, Message message){
        System.out.println("订单的ID:" + orderId);
        try {
            orderInfoService.updateOrderStatus(orderId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);//
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
